<template>
  <div class="tab-bar">
    <div class="tab-bar-item" v-for="(item, index) in tabbarData" :key="item.text" @click="clickItem(index, item)"
      :class="{ actived: curIndex === index }">
      <img v-if="index === curIndex" :src="getAssetImg(item.imgActive)" alt="">
      <img v-else :src="getAssetImg(item.img)" alt="">
      <span class="text">{{ item.text }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getAssetImg } from '../../utils/getAssetImg.js'
import { useRouter } from 'vue-router'
const tabbarData = [
  {
    text: '首页',
    img: 'tabbar/tab_home.png',
    imgActive: 'tabbar/tab_home_active.png',
    path: '/home'
  },
  {
    text: '收藏',
    img: 'tabbar/tab_favor.png',
    imgActive: 'tabbar/tab_favor_active.png',
    path: '/favor'
  },
  {
    text: '订单',
    img: 'tabbar/tab_order.png',
    imgActive: 'tabbar/tab_order_active.png',
    path: '/order'
  },
  {
    text: '消息',
    img: 'tabbar/tab_message.png',
    imgActive: 'tabbar/tab_message.png',
    path: '/message'
  },
]

// 当前下标
let curIndex = ref(0)

const router = useRouter()

function clickItem(index, item) {
  // 高亮
  curIndex.value = index
  // 路由跳转
  router.push(item.path)
}
</script>

<style scoped lang="less">
.tab-bar {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  border-top: 1px solid #333;

  .tab-bar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    &.actived {
      color: var(--primary-color);
    }

    img {
      width: 36px;
    }

    .text {
      font-size: 12px;
      margin-top: 2px;
    }
  }
}
</style>
